package com.lab.kit

import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit

object TimeKit {

    @JvmStatic fun format(time: Any, format: String): String {
        val sf = SimpleDateFormat(format, Locale.getDefault())
        return sf.format(time)
    }

    /**
     * 计算时间间隔
     */
    @JvmStatic fun interval(firstTime:Long,secondTime:Long):Long{
        return firstTime - secondTime
    }

    @JvmStatic fun toExpression(time:Long,minutes:Int,format: String):String{
        var timeExpression = ""
        if (time < TimeUnit.MINUTES.toMillis(1)){ //如果时间小于1分钟
            timeExpression = TimeUnit.MILLISECONDS.toSeconds(time).toString() + "秒"
        }else if (time< TimeUnit.MINUTES.toMillis(minutes.toLong())){ //如果时间小于10分钟
            timeExpression = TimeUnit.MILLISECONDS.toMinutes(time).toString() + "分钟"
        }
        return timeExpression+format
    }

    @JvmStatic fun isToday(time:Long):Boolean{
        val calendar = Calendar.getInstance()
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val date = calendar.get(Calendar.DATE)
        calendar.time = Date(time)

        val curYear = calendar.get(Calendar.YEAR)
        val curMonth = calendar.get(Calendar.MONTH)
        val curDate = calendar.get(Calendar.DATE)
        return (year==curYear) && (month==curMonth) && (date==curDate)
    }




}
